/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: Feb 16, 2012
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import model.Job;
import model.Volunteer;
import controller.Controller;

/**
 * This class shows the staff member panel. it begins by showing all jobs in the system 
 * ever. Then they are given the option to view jobs and black ball volunteers.
 * @author  Michael Pogson, Michael SatranJr
 *
 */
public class StaffPanel extends UserPanel {
	
	/**
	 * The frame that the staff panel is shown in.
	 */
	private JFrame my_frame;
	
	/**
	 * The controller that is used to manipulate the data.
	 */
	private Controller my_controller;
	
	/**
	 * The pane that holds all the content within the frame.
	 */
	private JPanel my_contentPane;
	
	private JTable my_jobs_table;
	
	/**
	 * A constructor that creates and sets up the fields.
	 * @param the_controller the controller that all the panels use.
	 */
	public StaffPanel(final Controller the_controller) {
		my_controller = the_controller;
		my_frame = new JFrame("Urban Parks - Staff Member");
		my_contentPane = new JPanel();
	}
	
	/**
	 * Used to get the frame to show on screen
	 */
	public void start() {
		super.start(my_frame, my_contentPane, my_controller);
	}
	
	/**
	 * This sets up all information on the screen and gives all the options
	 * to the user.
	 */
	@Override
	public void setup() {
		my_frame.setBounds(100, 100, 450, 300);
		my_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		my_contentPane.setLayout(new BorderLayout(0, 0));
		my_frame.setContentPane(my_contentPane);
		
		JPanel panel = new JPanel();
		my_contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnViewBlackBalled = new JButton("View Black Balled Volunteers");
		
		/*creates the action listener for the black ball button*/
		btnViewBlackBalled.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				/*Frame to show the black balled Volunteers*/
				final JFrame frame = new JFrame("Black Balled List");
				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				frame.setBounds(100, 100, 450, 300);
				my_contentPane = new JPanel();
				my_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
				my_contentPane.setLayout(new BorderLayout(0, 0));
				frame.setContentPane(my_contentPane);
				
				JPanel panel = new JPanel();
				my_contentPane.add(panel, BorderLayout.SOUTH);
				panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
				
				JButton btnReturnToStaff = new JButton("Return to Staff Member Page");
				btnReturnToStaff.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						frame.setVisible(false);
					}
				});
				
				panel.add(btnReturnToStaff);
				//panel.add(btnUnBlackBall);
				
				JScrollPane scrollPane = new JScrollPane();
				my_contentPane.add(scrollPane, BorderLayout.CENTER);
				
				JList<Volunteer> bblist = new JList<Volunteer>();
				bblist.setModel(new AbstractListModel<Volunteer>() {
					Object[] values = my_controller.getBlackBalledVolunteers().toArray(); 
					public int getSize() {
						return values.length;
					}
					public Volunteer getElementAt(int index) {
						return (Volunteer) values[index];
					}
				});
				
				bblist.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
				scrollPane.setViewportView(bblist);
				frame.setVisible(true);
			}
		});
		
		panel.add(btnViewBlackBalled);
		
		JButton btnReturnToSignIn = new JButton("Logout");
		
		/*Action listener for the return to sign in button*/
		btnReturnToSignIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_frame.dispose();
			}
			
		});
		
		panel.add(btnReturnToSignIn);
		

		JButton btnCalendar = new JButton("View Calendar");
		
		/*Action listener for the view calendar button*/
		btnCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Calendar cal = new GregorianCalendar();
				List<Job>jobs = new ArrayList<Job>(my_controller.getAllJobs());
				
				/*Frame for showing the calendar*/
				try {
					JFrame frame = new JFrame("Calendar");
					CalendarPanel calendar = new CalendarPanel(jobs, cal.get(cal.MONTH)+1 , cal.get(cal.YEAR), frame, my_controller);
					calendar.setup();
					frame.setBounds(100, 100, 450, 300);
					frame.setContentPane(calendar);
					frame.setVisible(true);
					frame.pack();
				} catch(IllegalArgumentException exception) {
					JOptionPane pane = new JOptionPane("No Volunteers!");
					pane.setVisible(true);
				} catch(Exception exception) {
					exception.printStackTrace();
				}
			}
			
		});
		
		panel.add(btnCalendar);
		
		//JScrollPane scrollPane = new JScrollPane();
		//my_contentPane.add(scrollPane, BorderLayout.CENTER);
		
		/*Label for total jobs*/
		JLabel jobsLabel = new JLabel();
		jobsLabel.setText("Total Open Jobs: " + Integer.toString(my_controller.getTotalJobs()));
		jobsLabel.setVisible(true);
		my_contentPane.add(jobsLabel, BorderLayout.NORTH);
		
		my_jobs_table = new JTable();
		setupTable(my_jobs_table, (List<Job>) my_controller.getAllJobs(), new String[] {"Jobs in the system"});
		my_contentPane.add(new JScrollPane(my_jobs_table));
		my_jobs_table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				//Frame for the job information
				JFrame infoframe = new JFrame("Urban Parks: Job Info");
				infoframe.setBounds(100, 100, 450, 300);
				JobInformationPanel infopanel = new JobInformationPanel((Job)my_jobs_table.getValueAt(my_jobs_table.getSelectedRow(), 0), my_controller);
				infopanel.setup();
				infoframe.add(infopanel);
				infoframe.setVisible(true);
				infoframe.pack();
			}
		});
	}

}
